<?php
require_once 'guestbook.class.php';
require_once 'guestbook.lang.php';
$g = new guestbook;
//if(!$M['show']) showmessage($LANG['message_disable'],'goback');

switch($action)
{	
	case 'post':
		//if(!$M['enableTourist'] && !$_userid) showmessage('请登录后再发表留言', url("?module=member&action=login&forward=".urlencode(URL)));
		if($dosubmit)
		{
			if(!$M['enableTourist'] && !$_username)
			{
				//showmessage($LANG['dis_guest'],'goback');
			}
			//if($M['enablecheckcode'])	
			//{
				checkcode($checkcodestr,1,$forward);
			//}
			if($gender!=0 && $gender!=1)
			{
				showmessage($LANG['illegal_parameters'],'goback'); 
			}
			if(strlen($username)>20 || strlen($email)>50 || strlen($homepage)>255)
			{
				showmessage($LANG['illegal_parameters'],'goback');
			}
			if(strlen($guestbook['content'])>200)
			{
				showmessage($LANG['message_more'].'200'.$LANG['message_characters'],'goback'); 
			}
			if(empty($guestbook['username']))
			{
				showmessage($LANG['message_input_username'],'goback');
			}
			if(empty($guestbook['title']))
			{
				showmessage($LANG['message_input_subject'],'goback');
			}
			if(empty($guestbook['content']))
			{
				showmessage($LANG['message_input_content']);
			}
			if($hidden!=0 && $hidden!=1)
			{
				showmessage($LANG['illegal_parameters'],'goback'); 
			}
		
			$username = $_username ? $_username : strip_tags($username);
			$inputstring=array('title'=>$title,'username'=>$username,'gender'=>$gender,'head'=>$headimg,'email'=>$email,'homepage'=>$homepage,'hidden'=>$hidden);
			extract($inputstring,EXTR_OVERWRITE);
			$title = strip_tags($title);
			//$content = $M['usehtml'] ? str_safe($content) : htmlspecialchars($content);
			if($M['checkpass'])
			{
				$passed = 0;
				$showmessage = $LANG['message_publishes_waite'];
			}
			else
			{
				$passed = 1;
				$showmessage = $LANG['message_publishes_successfully'];
			}
			$guestbook['passed'] = $passed;
			$guestbook['ip'] = IP;
			$guestbook['userid'] =$_userid;
			$guestbook['addtime'] = TIME;
			if($g->add($guestbook)) showmessage($showmessage,"?module=$module");
		}
		else
		{
			require 'form.class.php';
			$face = $g->get_face();
			$srchtype = isset($srchtype) ? $srchtype : 0;
			$keyword = isset($keyword) ? $keyword : '';
			$position = $M['name'];
			include template('post.html','guestbook'); 
		}
	break;	
	
	default:
		$pagesize = $M['pagesize'] ?  $M['pagesize'] : 10 ;
		$page = $page ? $page : 1 ;
		$gid = isset($gid) ? intval($gid) : 0;
		$srchtype = isset($srchtype) ? $srchtype : 0 ;
		$keyword = isset($keyword) ? $keyword : '' ;
		$condition = " and ((passed=1) or (username = '".$_username."' and passed=1 )) ";
		if(!empty($keyword))
		{
			$keyword=str_replace(' ','%',$keyword);
			$keyword=str_replace('*','%',$keyword);
			switch($srchtype)
			{
				case '0':
					$condition .=" AND title like '%$keyword%' ";
					break;
				case '1':
					$condition .=" AND content like '%$keyword%' ";
					break;
				case '2':
					$condition .=" AND username like '%$keyword%' ";
					break;
				default :
					$condition .=" AND title like '%$keyword%' ";
			}
		}
		$gbooks = array();
		if($gid) $condition .= " and gid=$gid";
		$glist = $g->listinfo($condition,$page,$pagesize); 
		$pages = $g->pages;
		
		$head['title'] = '留言板_'.$SETTING['sitename'];
		$head['keywords'] = $meta_keywords;
		$head['description'] = $meta_description;
		
		$ttl = $child == 1 ? CACHE_PAGE_CATEGORY_TTL : CACHE_PAGE_LIST_TTL;
		header('Last-Modified: '.gmdate('D, d M Y H:i:s', TIME).' GMT');
		header('Expires: '.gmdate('D, d M Y H:i:s', TIME + $ttl).' GMT');
		header('Cache-Control: max-age='.$ttl.', must-revalidate');
		include template('index.html','guestbook');
	break;
}
?>